﻿using System;
using System.Collections.Generic;
using System.Text;
using CLoggerNamespace;

namespace HL7v_2x
{

    public class HL7SubFields
    {
        /*
         * public int inCantApost=0;
            public string[] strSubField=null;
            List<HL7SubFields> lstSubFields=null;
         */
        public List<string> lstrSubFields=new List<string>();

        public HL7SubFields(byte[] byDataFieldReceived)       //constructor
        {
            System.Text.ASCIIEncoding enc = new ASCIIEncoding();
           // CLogger.Append("**DATA FIELD RECEIVED: " +  enc.GetString(byDataFieldReceived), CLogger.INFO);

            try
            {
                if (!enc.GetString(byDataFieldReceived).Contains("^"))
                {
                    this.lstrSubFields.Add(enc.GetString(byDataFieldReceived));
                }
                else if(enc.GetString(byDataFieldReceived).Contains("^~\\&"))
                {
                    this.lstrSubFields.Add(enc.GetString(byDataFieldReceived));
                }
                else
                {
                    int x = 0;
                    string[] strArrayAux;

                    strArrayAux = enc.GetString(byDataFieldReceived).Split('^');
                    //CLogger.Append("********Cantidad de SubCampos: " + strArrayAux.Length.ToString(), CLogger.INFO);

                    while (x < strArrayAux.Length)
                    {
                        CLogger.Append("SubFIELD " + x.ToString() + " : " + strArrayAux[x], CLogger.INFO);
                        this.lstrSubFields.Add(strArrayAux[x]);
                        x++;
                    }

                }
            }
            catch (Exception ex)
            {
                throw new Exception("EXCEPCION AL CREAR SUBFIELD - Ex: " + ex.Message);
            }
        }
    }
}
